package com.sosee.cgfw.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.entity.News;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.Calendar;
import java.util.List;

/**
 * Created by Thinkpad on 2016/1/25.
 */
public class FashionAdapter extends BaseAdapter {
    ImageLoader loader= ImageLoader.getInstance();
    DisplayImageOptions options;

    private BitmapUtils bitmapUtils;
    public List<News> getList() {
        return list;
    }

    public void setList(List<News> list) {
        this.list = list;
    }

    private List<News> list;
    private LayoutInflater inflater;
    public FashionAdapter(Context context){

        inflater=LayoutInflater.from(context);
        bitmapUtils=new BitmapUtils(context);
        options = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
                .build();//构建完成
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=inflater.inflate(R.layout.item_news,null);
            holder=new ViewHolder();
            holder.tv_title= (TextView) view.findViewById(R.id.tv_title);
            holder.ig= (ImageView) view.findViewById(R.id.ig_news);
            holder.tv_date= (TextView) view.findViewById(R.id.tv_date);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
        News news=list.get(i);
        holder.tv_title.setText(news.getTitle());
        String date=news.getDate();
        Calendar calNew=Calendar.getInstance();
        Calendar calDate=Calendar.getInstance();
        String[] da=date.split("-");
        calDate.set(calDate.get(Calendar.YEAR),Integer.parseInt(da[1])-1,Integer.parseInt(da[2]));
        long time=calNew.getTimeInMillis()-calDate.getTimeInMillis();
        int day=(int)(time/1000/3600/24);
        holder.tv_date.setText(date);
        loader.displayImage(Constans.BASE_URL+news.getPic(),holder.ig,options);
        LogUtils.log_E(day+"天前"+date);
        return view;
    }


    public class ViewHolder{
        TextView tv_title;//新闻列表标题
        ImageView ig;//新闻图片
        TextView tv_date;//发布日期
    }
}
